package com.wuhenzhizao.sku.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.wuhenzhizao.sku.R;
import com.wuhenzhizao.sku.bean.SkuAttribute;
import com.wuhenzhizao.sku.databinding.ViewSkuItemBinding;

/**
 * Created by wuhenzhizao on 2017/7/31.
 */

public class SkuItemView extends LinearLayout {
    // 背景圆角大小
    public int backgroundRadius = SizeUtils.dp2px(6);
    private String attributeValue; //具体色值，xl
    private String attributeValueDesc; //红色，尺码
    private String skuImage;
    private ViewSkuItemBinding mBinding;

    public SkuItemView(Context context) {
        super(context);
        init(context);
    }

    public SkuItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SkuItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setGravity(Gravity.CENTER);
        setBackgroundResource(R.drawable.sku_item_selector);
        mBinding = DataBindingUtil.bind(View.inflate(getContext(), R.layout.view_sku_item, null));
        addView(mBinding.getRoot());
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        mBinding.tvName.setEnabled(enabled);
    }

    public String getAttributeValue() {
        return attributeValue;
    }

    public String getAttributeValueDesc() {
        return attributeValueDesc == null ? "" : attributeValueDesc;
    }

    public String getSkuImage() {
        return skuImage == null ? "" : skuImage;
    }

    public void setAttributeValue(SkuAttribute skuAttribute) {
        this.attributeValue = skuAttribute.getValue();
        this.attributeValueDesc = skuAttribute.getValueDesc();
        skuImage = skuAttribute.getSkuImage();
        mBinding.tvName.setText(attributeValue);
        if (skuAttribute.getColorImg() != null && skuAttribute.getColorImg().length() > 0) {
            mBinding.ivSkuImg.setVisibility(View.VISIBLE);
            // 加载为四个都是圆角的图片 可以设置圆角幅度
            RequestOptions options = new RequestOptions()
                    .bitmapTransform(new RoundedCorners(backgroundRadius));
            Glide.with(getContext()).applyDefaultRequestOptions(options).load(skuAttribute.getColorImg()).into(mBinding.ivSkuImg);
        } else {
            mBinding.ivSkuImg.setVisibility(View.GONE);
        }
    }
}
